import Vue from 'vue'
import VueRouter from 'vue-router'

// 引入拆分的路由
import routes from './routes'

// 用户是否登录
import {isLogin} from "./user";


Vue.use(VueRouter)
// 路由栈 用于判断当前页面是否有上级页面
var routeStack = ["/"];
const router = new VueRouter({
  // mode: 'history',
  // base: process.env.BASE_URL,
  routes
})

// 路由守卫
//  
router.beforeEach((to,from,next)=>{
  if(to.meta.isAuth){
    if(isLogin()){
      next();
    }else{
      next()
    }
  }else{
    next()
  }
})

function repaceRoute(path){
  // 清空数组
  routeStack.splice(0,ary.length);
  // routeStack.pop();
  routeStack.push(path);
  router.replace(path)
}
// 跳转下级的两种方法
function pushRouteByQuery(path,params){
  routeStack.push(path);
  router.push({
    path:path,
    query:params
  })
}
function pushRouteByName(name,params){
  routeStack.push(name);
  router.push({
    name:name,
    params:params
  })
}
// 返回按钮
function backRoute(){
  routeStack.pop();
  router.back();
}



//配置方法到 router
router.repaceRoute = repaceRoute;
router.pushRouteByQuery = pushRouteByQuery;
router.pushRouteByName = pushRouteByName;
router.backRoute = backRoute;
router.routeStack = routeStack;

export default router
